package com.by.component.vod;

import com.by.component.vod.dto.CreateVodAppInfoSDTO;
import com.by.component.vod.mapper.VodAppInfoMapper;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.CreateSubAppIdRequest;
import com.tencentcloudapi.vod.v20180717.models.CreateSubAppIdResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Objects;

import static com.by.toolkit.utils.ExceptionUtil.getStackTrace;
import static com.by.toolkit.utils.ExceptionUtil.throwError;

@Slf4j
@DubboService
public class VodAppInfoServiceImpl implements VodAppInfoService {

    @Resource
    private VodAppInfoMapper vodAppInfoMapper;

    @Resource
    private VodClient vodClient;

    @Override
    public void createVodAppId(CreateVodAppInfoSDTO d) {
        String corpName = d.getCorpName();
        Long projectId = d.getProjectId();
        Long vodAppId = vodAppInfoMapper.vodAppId(projectId);
        if (Objects.nonNull(vodAppId)) {
            return;
        }

        CreateSubAppIdRequest req = new CreateSubAppIdRequest();
        req.setName(corpName);
        try {
            CreateSubAppIdResponse resp = vodClient.CreateSubAppId(req);
            Long subAppId = resp.getSubAppId();
            vodAppInfoMapper.insert(VodAppInfo.builder()
                            .vodAppId(subAppId)
                            .vodAppName()
                            .vodAppType()
                    .build());
        } catch (TencentCloudSDKException e) {
            log.warn("[VodAppInfoServiceImpl][createVodAppId] hasException:[{}] ", getStackTrace(e));
            throwError("创建vodApp异常");
        }
    }

}
